package com.wqj.kyee.gps.adapter;

import android.annotation.SuppressLint;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Poi;
import com.wqj.kyee.gps.R;
import com.wqj.kyee.gps.nav.MapActivity;
import com.wqj.kyee.gps.nav.RouteShowActivity;
import com.wqj.kyee.gps.ui.SearchActivity;
import com.wqj.kyee.gps.util.BaseTransitionInActivity;
import com.wqj.kyee.gps.util.Constants;

import java.util.List;

/**
 * Created by Administrator on 2018/1/8.
 */

public class CustomLocationAdapter extends BaseAdapter implements AdapterView.OnClickListener {

    private MapActivity mContext;
    private List<String> mCustomList;
    private LayoutInflater layoutInflater;
    //private PopupWindow mPopupWindow;

    public CustomLocationAdapter(MapActivity context, List<String> history /*, PopupWindow popupWindow*/)
    {
        mContext = context;
        mCustomList = history;
        layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //mPopupWindow = popupWindow;
    }

    @Override
    public int getCount() {
        if (mCustomList != null) {
            return mCustomList.size();
        }
        return 0;
    }


    @Override
    public Object getItem(int i) {
        if (mCustomList != null) {
            return mCustomList.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("RestrictedApi")
    private void startActivityWithOptions(Intent intent, int requestCode) {
        ActivityOptions transitionActivity =
                ActivityOptions.makeSceneTransitionAnimation(mContext);
        mContext.startActivityForResult(intent, requestCode, transitionActivity.toBundle());
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        try {
            CustomLocationAdapter.Holder holder;
            if (view == null) {
                holder = new CustomLocationAdapter.Holder();
                view = layoutInflater.inflate(R.layout.custom_location_list_item, null);
                holder.mName = (TextView) view.findViewById(R.id.name);
                holder.mAddress = (TextView) view.findViewById(R.id.address);
                holder.mEdit = (ImageButton) view.findViewById(R.id.id_ib_startEdit);
                view.setTag(holder);
            } else {
                holder = (CustomLocationAdapter.Holder) view.getTag();
            }
            if (mCustomList == null) {
                return view;
            }

            holder.mEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (position == 0) {
                        Intent intent;
                        intent = new Intent(mContext, SearchActivity.class);
                        intent.putExtra(BaseTransitionInActivity.EXTRA_TRANSITION, BaseTransitionInActivity.TRANSITION_FADE_FAST);
                        intent.putExtra(Constants.EXTRA_CURRENT_CITY, mContext.getCurrentCity());
                        intent.putExtra(Constants.EXTRA_HINT, "家庭地址");
                        intent.putExtra(Constants.EXTRA_POINTTYPE, Constants.EXTRA_POI_TYPE_HOME);
                        startActivityWithOptions(intent, Constants.SEARCH_REQUSET_HOME_CODE);
                    } else if (position == 1) {
                        Intent intent;
                        intent = new Intent(mContext, SearchActivity.class);
                        intent.putExtra(BaseTransitionInActivity.EXTRA_TRANSITION, BaseTransitionInActivity.TRANSITION_FADE_FAST);
                        intent.putExtra(Constants.EXTRA_CURRENT_CITY, mContext.getCurrentCity());
                        intent.putExtra(Constants.EXTRA_HINT, "公司地址");
                        intent.putExtra(Constants.EXTRA_POINTTYPE, Constants.EXTRA_POI_TYPE_COMPANY);
                        startActivityWithOptions(intent, Constants.SEARCH_REQUEST_COMPANY_CODE);
                    }

                    //dismiss()
                }
            });

            String str = mCustomList.get(position);
            String[] fields = str.split(";");

            holder.mName.setText(fields[0]);
            holder.mAddress.setText(fields[1]);

            holder.mAddress.setOnClickListener(new View.OnClickListener(

            ) {
                @Override
                public void onClick(View view) {
                    SharedPreferences sp = mContext.getSharedPreferences(Constants.CustomLocation_Preferences, Context.MODE_PRIVATE);
                    String item = "";
                    if (position == 0) {
                        item = sp.getString(Constants.SP_KEY_LOCATION_HOME, "");

                    } else if (position == 1) {
                        item = sp.getString(Constants.SP_KEY_LOCATION_COMPANY, "");
                    }

                    if (!item.isEmpty()) {

                        String[] fields = item.split(";");

                        String[] latlng = fields[1].split(",");

                        Poi poi = new Poi(fields[0], new LatLng(Double.parseDouble(latlng[0]), Double.parseDouble(latlng[1])), "");

                        Intent intent = new Intent(mContext, RouteShowActivity.class);
                        intent.putExtra(Constants.EXTRA_CURRENT_LAT, mContext.getCurrentLat());
                        intent.putExtra(Constants.EXTRA_CURRENT_LNG, mContext.getCurrentLng());
                        intent.putExtra(Constants.EXTRA_CURRENT_CITY, mContext.getCurrentCity());

                        intent.putExtra(Constants.EXTRA_POI, poi);
                        mContext.startActivity(intent);
                    }
                }
            });
            // String address = mListTips.get(i).getAddress();
//            if (TextUtils.isEmpty(address)) {
//                holder.mAddress.setVisibility(View.GONE);
//            } else {
//                holder.mAddress.setVisibility(View.VISIBLE);
//                holder.mAddress.setText(address);
//            }


        } catch (Throwable e) {
            e.printStackTrace();
        }
        return view;
    }

    @Override
    public void onClick(View view) {

    }

    class Holder {
        TextView mName;
        TextView mAddress;
        ImageButton mEdit;
    }
}

